<?php ob_start();
session_start(); // on relaye la session 
if(isset($_SESSION["is_registered"]) && $_SESSION["is_registered"] == "registered"){
		$enregistrer = $_SESSION["is_registered"];
}else{
	header("location:../../../menu.php"); // redirection en cas d'echec
}
require_once('../../../Connections/microassur.php');
require_once('../../../utils/fonctions.php'); require_once("../inclure.php"); ?>
<?php $message = ''; ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

$colname_rs_eltdico = "-1";
if (isset($_GET['Identifiant_element'])) {
  $colname_rs_eltdico = $_GET['Identifiant_element'];
}
mysql_select_db($database_microassur, $microassur);
$query_rs_eltdico = sprintf("SELECT * FROM elementdico WHERE Identifiant_element = %s", GetSQLValueString($colname_rs_eltdico, "int"));
$rs_eltdico = mysql_query($query_rs_eltdico, $microassur) or die(mysql_error());
$row_rs_eltdico = mysql_fetch_assoc($rs_eltdico);
$totalRows_rs_eltdico = mysql_num_rows($rs_eltdico);

mysql_select_db($database_microassur, $microassur);
$query_rs_dico = "SELECT * FROM dictionnaire";
$rs_dico = mysql_query($query_rs_dico, $microassur) or die(mysql_error());
$row_rs_dico = mysql_fetch_assoc($rs_dico);
$totalRows_rs_dico = mysql_num_rows($rs_dico);

// Vérifier si Identifiant_table (la table rattachée) est modifié
//$comparer2 = strcmp(@$_POST['Identifiant_table'], @$row_rs_dico['Identifiant_table']);

// Vérifier si Code_element est modifié
if(isset($_POST['Code_element']) && isset($row_rs_eltdico['Code_element'])){
	$comparer = strcmp($_POST['Code_element'], $row_rs_eltdico['Code_element']);
}

// Vérifier si la table est une table technique
if(isset($row_rs_eltdico['Identifiant_table'])){
	$isTechnique = isTableTechnique($database_microassur, $microassur, $row_rs_eltdico['Identifiant_table']);
	//var_dump($isTechnique);
}

// Vérifier si un même code existe déjà dans la table conditionvente
if(isset($_POST['Code_element'])){
	$existe = existeChamp($database_microassur, $microassur, 'elementdico', 'Code_element', $_POST['Code_element']);
}

if(isset($comparer) && $comparer && strcmp($existe, '0')!=0){// strcmp($_POST['Code_element'], @$row_rs_eltdico['Code_element'])){
	$message = colorer("Cet élément  dictionnaire existe déjà","false");
}elseif(isset($_POST['Code_element']) && empty($_POST['Code_element'])){
	$message = colorer("Vous devez saisir le CODE.","false");
}elseif(isset($_POST['Code_element']) && !veriflibelle1('Code_element') && caracteres_interdits($_POST['Code_element'])==1){
	$message = colorer("Le code saisi comporte des caractères spéciaux interdits.","false");
}elseif(isset($_POST['Nom_element']) && empty($_POST['Nom_element'])){
	$message = colorer("Vous devez saisir le nom.","false");
}elseif(isset($_POST['Nom_element']) && caracteres_interdits($_POST['Nom_element'])==1){
	$message = colorer("Le nom saisi comporte des caractères spéciaux interdits.","false");
}
elseif ( isset($_POST['Code_element']) && isset($row_rs_eltdico['Code_element']) && isset($comparer) && $comparer && isset($isTechnique) && $isTechnique ){
	$message = colorer("Vous ne pouvez pas modifier le CODE de cet élément","false");
} // Fin if($comparer)
else{
	if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
	  $updateSQL = sprintf("UPDATE elementdico SET Code_element=%s, Nom_element=%s, Identifiant_table=%s WHERE Identifiant_element=%s",
						   GetSQLValueString($_POST['Code_element'], "text"),
						   GetSQLValueString($_POST['Nom_element'], "text"),
						   GetSQLValueString($_POST['Identifiant_table'], "int"),
						   GetSQLValueString($_POST['Identifiant_element'], "int"));
	
	  mysql_select_db($database_microassur, $microassur);
	   mysql_query("START TRANSACTION");
	  $Result1 = mysql_query($updateSQL, $microassur) or die(mysql_error());
	  if($Result1){
	  mysql_query("commit");
			$message = colorer("La modification de l'élément dictionnaire est prise en compte","true");
			if($_POST['Code_element']!=$row_rs_eltdico['Code_element'] && !$isTechnique)
				$row_rs_eltdico['Code_element'] = $_POST['Code_element'];
			if($_POST['Nom_element']!=$row_rs_eltdico['Nom_element'])
				$row_rs_eltdico['Nom_element'] = $_POST['Nom_element'];		
			if($_POST['Identifiant_table']!=$row_rs_eltdico['Identifiant_table'])
				$row_rs_eltdico['Identifiant_table'] = $_POST['Identifiant_table'];
		}
	  else
		{ mysql_query("rollback");$message = colorer("Contacter la DSI, erreur modification dictionnaire","false");	}  	
	}
} // Fin else $comparer

?>
<?php 
	if(isset($message) && !empty($message)){
		//echo $message;
	}elseif(isset($_GET['msg']) && !empty($_GET['msg'])){
		$message = $_GET['msg'];
		switch($message){
			case "supp" :
				$message = colorer("L’élément dictionnaire est supprimé","true");
				break;
			case "err_supp" :
				$message = colorer("Contacter la DSI, erreur suppression élément dictionnaire","false");
				break;
			case "supp" :
				$message = colorer("L'élément dictionnaire est supprimé","true");
				break;
			default :
				$message = colorer("Contacter la DSI, erreur suppression élément dictionnaire","false");
		}
		//echo $message;
	}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=8" />
			<title><?php echo $titleGen ?></title>
			<link rel="stylesheet" type="text/css" href="../../../css/main.css" />
            <link rel="stylesheet" type="text/css" href="list.css" />
			<!--[if IE 7]>
            	<link rel="stylesheet" type="text/css" href="../../../css/ie7.css" />
            	<link rel="stylesheet" type="text/css" href="form_ie7.css" />
            <![endif]-->
	</head>
<body>
			<div id="sf_menu1">
				<h3>Administration</h3>
				<?php
					if(isset($message))
						echo $message;
					$nommodule="Elément dictionnaire";
					$nommodule2="Détail élément dict.";
					?>
			</div>
			<div class="cleared"></div>
			<!-- /*******************************/ -->
			<div class="corps1">
			<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
			<div class="imgleft"></div>
			<div class="imgmiddle">
				<div class="btn"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule; ?></h5></span><span class="btnright"></span></div>
				<div class="btn1"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule2; ?></h5></span><span class="btnright"></span></div>
					<div class="formulaire">
						  <table class="tabform">
							<tr>
							  <td>CODE</td>
							  <td><input class="resize_1" type="text" name="Code_element" value="<?php if(isset($_POST['Code_element'])){ echo remind_text('Code_element'); }else{ echo htmlentities($row_rs_eltdico['Code_element'], ENT_COMPAT, 'utf-8');} ?>" maxlength="5" size="32" /></td>
							</tr>
							<tr>
							  <td>NOM</td>
							  <td><input type="text" name="Nom_element" value="<?php if(isset($_POST['Nom_element'])){ echo remind_text('Nom_element'); }else{ echo htmlentities($row_rs_eltdico['Nom_element'], ENT_COMPAT, 'utf-8');} ?>"  maxlength="30" size="32" /></td>
							</tr>
							<tr>
							  <td>DICTIONNAIRE</td>
							  <td>
							<select name="Identifiant_table">
								<?php
						do {  
						?>
								<option value="<?php echo $row_rs_dico['Identifiant_table']?>"<?php if(isset($_POST['Identifiant_table'])){if (!(strcmp($row_rs_dico['Identifiant_table'], htmlentities($_POST['Identifiant_table'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";}}else{if (!(strcmp($row_rs_dico['Identifiant_table'], htmlentities($row_rs_eltdico['Identifiant_table'], ENT_COMPAT, 'utf-8')))) {echo "selected=\"selected\"";}} ?>><?php echo $row_rs_dico['Libelle_table']?></option>
								<?php
						} while ($row_rs_dico = mysql_fetch_assoc($rs_dico));
						  $rows = mysql_num_rows($rs_dico);
						  if($rows > 0) {
							  mysql_data_seek($rs_dico, 0);
							  $row_rs_dico = mysql_fetch_assoc($rs_dico);
						  }
						?>
							</select>
						  </td>
						</tr>
										</table>
									</div>
								</div>
								<div class="imgright"></div>				
									<div class="cleared"></div>
									
										<div class="bt1"><a href="./index.php"><span class="btleft"></span><span class="btmiddle">Retour</span><span class="btright"></span></a></div>
										<div class="bt"><a <?php if(isset($row_rs_eltdico['Identifiant_element'])){ ?> href="suppelementdico.php?Identifiant_element=<?php echo $row_rs_eltdico['Identifiant_element']; ?>" <?php } else echo 'href="#"'; ?> ><span class="btleft"></span><span class="btmiddle">Supprimer</span><span class="btright"></span></a></div>
										<div class="bt"><span class="btleft"></span><input <?php if(retourValForm('Identifiant_element')==NULL || retourValForm('msg')=='supp') echo "disabled='disabled'"; ?> type="submit" name="Modifier" value="Modifier" class="resizeall btmiddle"/><span class="btright"></span></div>
										
									<div class="cleared"></div>
									
					  <input type="hidden" name="Identifiant_element" value="<?php echo $row_rs_eltdico['Identifiant_element']; ?>" />
					  <input type="hidden" name="MM_update" value="form1" />
			</form>
			</div>
</body>
</html>
<?php
mysql_free_result($rs_eltdico);
mysql_free_result($rs_dico);
?>
<?php ob_end_flush();?>